package com.example.e_book.Adapter;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerAdapter extends FragmentStateAdapter {
    private final List<Fragment> fragmentList = new ArrayList<>();
    private final List<String> titleList = new ArrayList<>();
    private final List<Bundle> argsList = new ArrayList<>();

    public ViewPagerAdapter(FragmentActivity fa) {
        super(fa);
    }

    public void addFragment(Fragment fragment, String title, Bundle args) {
        fragmentList.add(fragment);
        titleList.add(title);
        argsList.add(args);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        Fragment fragment = fragmentList.get(position);
        if (argsList.size() > position && argsList.get(position) != null) {
            fragment.setArguments(argsList.get(position));
        }
        return fragment;
    }

    @Override
    public int getItemCount() {
        return fragmentList.size();
    }

    public CharSequence getTitle(int position) {
        return titleList.get(position);
    }
}